﻿using System;
using System.Collections.Generic;
using System.Text;
using HTML;
using System.Net;
using System.IO;

namespace SharpDownloadSDK
{
    public class HtmlParseUtilities
    {
        public static String GetDomain(String url)
        {           
            String prefix = "https://";
            int index = url.IndexOf('/', prefix.Length);
            if (index > 0)
            {
                return url.Substring(0, index);
            }
            return String.Empty;
        } 

        public static string GetPage(string url)
        {
            WebResponse response = null;
            Stream stream = null;
            StreamReader
              reader = null;

            try
            {
                HttpWebRequest request =
                               (HttpWebRequest)WebRequest.Create(url);

                response = request.GetResponse();
                stream = response.GetResponseStream();

                if (!response.ContentType.ToLower().StartsWith("text/"))
                    return null;

                string buffer = "", line;

                reader = new StreamReader(stream);

                while ((line = reader.ReadLine()) != null)
                {
                    buffer += line + "\r\n";
                }

                return buffer;
            }
            catch (WebException e)
            {                
                return null;
            }
            catch (IOException e)
            {                
                return null;
            }
            finally
            {
                if (reader != null)
                    reader.Close();

                if (stream != null)
                    stream.Close();

                if (response != null)
                    response.Close();
            }
        }

        public static void GetAllAdressesByUrlAndExts(String url, String extensions, ref List<String> allAdresses)
        {
            string page = GetPage(url);
            if (String.IsNullOrEmpty(page))
                return ;

            ParseHTML parse = new ParseHTML();
            parse.Source = page;
            while (!parse.Eof())
            {
                char ch = parse.Parse();
                if (ch == 0)
                {
                    AttributeList tag = parse.GetTag();
                    if (tag["href"] == null)
                        continue;
                    String value = tag["href"].Value;
                    if (!String.IsNullOrEmpty(value))
                    {
                        if (String.IsNullOrEmpty(extensions))
                        {
                            allAdresses.Add(value);
                        }
                        else
                        {
                            int index = value.LastIndexOf('.');
                            if (index > 0)
                            {
                                String ext = value.Substring(index, value.Length - index);
                                if (extensions.Contains(ext))
                                    allAdresses.Add(value);
                            }
                        }
                    }
                }
            }
        }
    }
}
